<?php

function show_tabs($cm, $context, $currenttab='view') {
    $toprow = array();
    $toprow[] = new tabobject('view', 'view.php?id=' . $cm->id,
                    get_string('view', 'studentcp'));
    $toprow[] = new tabobject('schedule', 'schedule.php?id=' . $cm->id,
                    get_string('schedule', 'studentcp'));
    $toprow[] = new tabobject('exam', 'exam.php?id=' . $cm->id,
                    get_string('exam', 'studentcp'));
    $toprow[] = new tabobject('grade', 'gradeview.php?id=' . $cm->id,
                    get_string('grade', 'studentcp'));
//    $toprow[] = new tabobject('attendance', 'attendance.php?id=' . $cm->id,
//                    get_string('attendance', 'studentcp'));
//    $toprow[] = new tabobject('activity', 'activity.php?id=' . $cm->id,
//                    get_string('activity', 'studentcp'));
    if (has_capability('mod/studentcp:managesubject', $context)) {
        $toprow[] = new tabobject('subjectmanagement', 'subjectmanagement.php?id=' . $cm->id,
                        get_string('subjectmanagement', 'studentcp'));
    }
    $tabs = array($toprow);
    print_tabs($tabs, $currenttab);
}

function week_day($month, $year, $current_day, $week_day_number=1) {

    $loop_start = $current_day - (date('N', mktime(0, 0, 0, $month, $current_day, $year)) - 1); //lets start loop from first day of week
    $loop_end = $current_day + (7 - (date('N', mktime(0, 0, 0, $month, $current_day, $year)))); //lets end loop to last day of week

    for ($i = $loop_start; $i <= $loop_end; $i++) {

        $day_of_the_week = date('N', mktime(0, 0, 0, $month, $i, $year)); //current day number 1-7 of week
        $loop_date = date('d', mktime(0, 0, 0, $month, $i, $year)); //current day in calendar
        if ($day_of_the_week == $week_day_number) {//if weekday number equals day number then lets return date
            return $loop_date;
        }
    }
}

function print_grade_report($subjectid, $cm) {
    $subject = get_record('studentcp_subject', 'id', $subjectid);
    $grades = get_records('studentcp_grade', 'subjectid', $subjectid);
    print_heading($subject->name);
    echo "<center>";
    print_headline($subject->description, 12);
    echo "</center>";
    unset($table); {
        $firstname = get_string('firstname');
        $lastname = get_string('lastname');
        if ($CFG->fullnamedisplay == 'lastname firstname') { // for better view (dlnsk)
            $fullnamehead = "$lastname / $firstname";
        } else {
            $fullnamehead = "$firstname / $lastname";
        }
        $table->head[] = $fullnamehead;
        $table->align[] = 'center';
        $table->size[] = '';
        $gradedescs = explode(";", $subject->gradedesc);
        $gradetable = explode(";", $subject->gradetable);

        echo $subject->grade;
        $gradenum = 0;
        foreach ($gradedescs as $gradedesc) {

            $table->head[] = $gradedesc . " - " . $gradetable[$gradenum] . "%";

            $table->align[] = 'center';
            $table->size[] = '';
            //$table->wrap[2] = 'nowrap';
            $gradenum++;
        }
        $table->head[] = get_string('average', 'studentcp');
        $table->align[] = 'center';
        $table->size[] = '';
        $ii = 0;
        foreach ($grades as $grade) {
            $student = get_record('user', 'id', $grade->userid);
            $table->data[$ii][] = "<a href=\"gradeview.php?id={$cm->id}&student={$student->id}\">" . fullname($student) . "</a>";
            $gradedatas = explode(";", $grade->gradedata);
            $gradenum = 0;
            $average = 0;
            foreach ($gradedatas as $gradedata) {
                $table->data[$ii][] = $gradedata;
                $average += ( $gradedata * $gradetable[$gradenum]) / 100;

                $gradenum++;
            }
            $table->data[$ii][] = $average;
            $ii++;
        }
    }
    print_table($table);
    echo "<center>";
    print_single_button("subjectmanagement.php",array('id'=>$cm->id), 'Back');
    echo "</center>";
}

function print_grade_table($grade) {
    $subject = get_record('studentcp_subject', 'id', $grade->subjectid);
    print_heading($subject->name);
    print_headline($subject->description, 12);
    $average = 0;
    unset($table); {
        $table->head[] = get_string('credit', 'studentcp');
        $table->align[] = 'center';
        $table->size[] = '';
        //$table->wrap[2] = 'nowrap';
        $gradedescs = explode(";", $subject->gradedesc);
        $gradetable = explode(";", $subject->gradetable);

        echo $subject->grade;
        $gradenum = 0;
        foreach ($gradedescs as $gradedesc) {

            $table->head[] = $gradedesc . " - " . $gradetable[$gradenum] . "%";

            $table->align[] = 'center';
            $table->size[] = '';
            //$table->wrap[2] = 'nowrap';
            $gradenum++;
        }
        $table->head[] = get_string('average', 'studentcp');
        $table->align[] = 'center';
        $table->size[] = '';
        $table->data[0][] = $subject->credit;
        $gradedatas = explode(";", $grade->gradedata);

        $gradenum = 0;
        foreach ($gradedatas as $gradedata) {
            $table->data[0][] = $gradedata;
            $average += ( $gradedata * $gradetable[$gradenum]) / 100;

            $gradenum++;
        }
        $table->data[0][] = $average;
        $return['credit']=$subject->credit;
        $return['average']=$average;
    }
    print_table($table);
    return $return;
}

function print_subject_selector($gradelist, $semester=0, $defaultsubject=0, $return=false) {
    $subject_list['']='';

    foreach ($gradelist as $grade) {
        $subject = get_record('studentcp_subject', 'id', $grade->subjectid);
        if ($semester) {
            if ($grade->semester == $semester)
                $subject_list[$grade->subjectid] = $subject->name;
        } else {
            $subject_list[$grade->subjectid] = $subject->name;
        }
    }
    $result = '';

    $result.='<label class="accesshide" for="menuget_subject">' . get_string('studentcp', 'subject') . '</label>';
    $result.=choose_from_menu($subject_list, 'get_subject', $defaultsubject, '', '', '0', true, false, 0, '', false);
    if ($return) {
        return $result;
    } else {
        echo $result;
    }
}


function print_month_selector($gradelist, $currenttime=0, $return=false) {
    $semester_list['1']= get_string("spring", "studentcp");
    $semester_list['2']= get_string("spring", "studentcp");
    $semester_list['3']= get_string("spring", "studentcp");
    $semester_list['4']= get_string("spring", "studentcp");
    $semester_list['5']= get_string("spring", "studentcp");
    $semester_list['6']= get_string("spring", "studentcp");
    $semester_list['7']= get_string("spring", "studentcp");
    $semester_list['8']= get_string("spring", "studentcp");
    $semester_list['9']= get_string("spring", "studentcp");
    $semester_list['10']= get_string("spring", "studentcp");
    $semester_list['11']= get_string("spring", "studentcp");
    $semester_list['12']= get_string("spring", "studentcp");

    if (!$currenttime) {
        $currenttime = time();
    }
    $currentdate = usergetdate($currenttime);
    $result='';
    $semesters[0] = ' ';
    foreach ($gradelist as $grade)
    {
        //convert Unix time list to Semester list;
        $semester = $grade->semester;
        $semesterdate = usergetdate($semester);
//        $semester_name = $semester_list[$semesterdate['mon']] . '-' . $semesterdate['year'] ;
        $semester_name = $semesterdate['mon'] . '-' . $semesterdate['year'] ;
        $semesters[$semester] = $semester_name;
    }


    // Build or print result
    
    // Note: There should probably be a fieldset around these fields as they are
    // clearly grouped. However this causes problems with display. See Mozilla
    // bug 474415
    
    $result.='<label class="accesshide" for="menu_get_semester">semester</label>';
    $result.=choose_from_menu($semesters,  'get_semester',  $currenttime, '', '', '0', true);

    if ($return) {
        return $result;
    } else {
        echo $result;
    }
}

function my_file_get_contents($filename, $use_include_path = 0) {
/// Returns the file as one big long string
    $data = "";
    $file = @fopen($filename, "rb", $use_include_path);
    if ($file) {
        while ($line = fgetcsv($file, 1024, ";")) {
//            $line= fread($file, 1024);
            $data[] = $line;
        }
        fclose($file);
    }
    return $data;
}

?>
